package org.justdroid.justdroid;

import java.util.ArrayList;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources.NotFoundException;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.SectionIndexer;
import android.widget.TextView;

@SuppressWarnings("rawtypes")
public class ContactsAdapter extends ArrayAdapter implements SectionIndexer {
	private final Context context;
	private ArrayList<String> values;
	
	@SuppressWarnings("unchecked")
	public ContactsAdapter(Context context, ArrayList values) {
		super(context, R.layout.row,values);
		this.context = context;
		this.values = new ArrayList();
		this.values = (values);
	}
	
	//this method generates a View for a list item	
	public View getView(int position, View view, ViewGroup parent)
	{
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowView = inflater.inflate(R.layout.row, parent, false);
		final TextView textView = (TextView) rowView.findViewById(R.id.textView1);
		textView.setText(values.get(position));
		textView.setContentDescription(values.get(position));
		SharedPreferences preferences = context.getSharedPreferences(context.getResources().getString(R.string.color), 0);
		if(preferences.getInt("bgcolor", 0) != 0 || preferences.getInt("fgcolor", 0) != 0) {
			int bgColor = preferences.getInt("bgcolor", 0);
			int fgColor = preferences.getInt("fgcolor", 0);
			try{
				context.getResources().getResourceName(bgColor);
				bgColor = context.getResources().getColor(bgColor);
			}
			catch(NotFoundException nfe) {
				bgColor = context.getResources().getColor(R.color.homescreen_background);
			}
			try{
				context.getResources().getResourceName(fgColor);
				fgColor = context.getResources().getColor(fgColor);
			}
			catch(NotFoundException nfe) {
				fgColor = context.getResources().getColor(R.color.card_textcolor_regular);
			}
			textView.setTextColor(fgColor);
			textView.setBackgroundColor(bgColor);
			
			preferences = context.getSharedPreferences(context.getResources().getString(R.string.fonttype), 0);
			if(preferences.getInt("typeface", -1) != -1) {
				switch(preferences.getInt("typeface", -1)) {
				case Utils.NONE:
					textView.setTypeface(null, Typeface.BOLD);
					break;
				case Utils.SERIF:
					textView.setTypeface(Typeface.SERIF);
					break;
				case Utils.MONOSPACE:
					textView.setTypeface(Typeface.MONOSPACE);
					break;
				}
			}
			else {
				textView.setTypeface(null, Typeface.BOLD);
			}
			
			preferences = context.getSharedPreferences(context.getResources().getString(R.string.size), 0);	
			if(preferences.getFloat("size", 0) != 0) {
				float fontSize = preferences.getFloat("size", 0);
				textView.setTextSize(fontSize);
			}
			else {
				textView.setTextSize(context.getResources().getDimension(R.dimen.card_textsize_regular));
			}
			
		}
		return rowView;
	}

	@Override
	public int getPositionForSection(int i) {
		 return (int) (getCount() * ((float)i/(float)getSections().length));
	}

	@Override
	public int getSectionForPosition(int arg0) {
		return 0;
	}

	@Override
	public Object[] getSections() {
		 String[] chars = new String[SideSelector.ALPHABET.length];
         for (int i = 0; i < SideSelector.ALPHABET.length; i++) {
             chars[i] = String.valueOf(SideSelector.ALPHABET[i]);
         }

         return chars;
	}
}
